package com.skhu.as.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.command.FaultHandlingRegCmd;
import com.skhu.as.domain.DutyHours;
import com.skhu.as.domain.FaultHandling;
import com.skhu.as.domain.ProcessStatus;
import com.skhu.as.domain.RequestType;
import com.skhu.as.domain.Requester;
import com.skhu.as.domain.Worker;
import com.skhu.as.service.FaultHandlingS;
import com.skhu.as.util.DateU;

public class FaultHandlingRegC extends SimpleFormController {

	private FaultHandlingS faultHandlingS;
	
	public void setFaultHandlingS(FaultHandlingS faultHandlingS) {
		this.faultHandlingS = faultHandlingS;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		FaultHandlingRegCmd cmd = (FaultHandlingRegCmd)command;
		DateU dateU = new DateU();
		
		/**
		 * 장애처리 등록 전 의뢰자 등록 먼저
		 */
		Requester requester = new Requester();
		int requesterNo = faultHandlingS.getRequesterMaxNo() + 1;
		requester.setRequesterNo(requesterNo);
		requester.setName(cmd.getRequesterName());
		requester.setContactNumber(cmd.getRequesterContactNumber());
		requester.setRequestPlace(cmd.getRequestPlace());
		requester.setDepartment(cmd.getDepartment());
		
		faultHandlingS.insertRequester(requester);
		
		String year = cmd.getProcessYear().length() < 2 ? "0" + cmd.getProcessYear() : cmd.getProcessYear();
		String month = cmd.getProcessMonth().length() < 2 ? "0" + cmd.getProcessMonth() : "" + cmd.getProcessMonth();
		String dayOfMOnth = cmd.getProcessDayOfMonth().length() < 2 ? "0" + cmd.getProcessDayOfMonth() : "" + cmd.getProcessDayOfMonth();
		
		/**
		 * 장애처리 등록
		 */
		FaultHandling faultHandling = new FaultHandling();
		int faultHandlingMaxNo = faultHandlingS.getFaultHandlingMaxNo() + 1;
		faultHandling.setDutyHoursNo(cmd.getDutyHoursNo());
		faultHandling.setFaultHandlingNo(faultHandlingMaxNo);
		faultHandling.setFirstWorkerNo(cmd.getFirstWorkerNo());
		faultHandling.setProcessDate(year + "" + month + "" + dayOfMOnth);
		faultHandling.setProcessingInformation(cmd.getProcessingInformation());
		faultHandling.setProcessStatusNo(cmd.getProcessStatusNo());
		faultHandling.setReceiptDate(dateU.getDateTime());
		faultHandling.setReceptionistNo(cmd.getReceptionistNo());
		faultHandling.setRequestContent(cmd.getRequestContent());
		faultHandling.setRequesterNo(requesterNo);
		faultHandling.setRequestTypeNo(cmd.getRequestTypeNo());
		
		faultHandlingS.insertFaultHandling(faultHandling);
		
		return super.onSubmit(request, response, command, errors);
	}
	
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		
		Map ref = new HashMap();
		
		/**
		 * 작업자 목록
		 */
		List<Worker> workerList = faultHandlingS.getWorkerList();
		ref.put("workerList", workerList);
		
		/**
		 * 오늘 날짜 생성
		 */
		DateU dateU = new DateU();
		String today = dateU.getDate();
		ref.put("today", today);
		ref.put("processYear", dateU.getYear());
		ref.put("processMonth", dateU.getMonth());
		ref.put("processDayOfMonth", dateU.getDayOfMonth());
		
		/**
		 * 근무시간
		 */
		List<DutyHours> dutyHoursList = faultHandlingS.getDutyHoursList();
		ref.put("dutyHoursList", dutyHoursList);
		
		/**
		 * 요청구분
		 */
		List<RequestType> requestTypeList = faultHandlingS.getRequestTypeList();
		ref.put("requestTypeList", requestTypeList);
		
		/**
		 * 처리상태
		 */
		List<ProcessStatus> processStatusList = faultHandlingS.getProcessStatusList();
		ref.put("processStatusList", processStatusList);
		
		
		return ref;
	}
	
}
